#include "./compile_run.hpp"
#include "../common/httplib.h"

using namespace ns_compiler_runner;

void test_CompileAndRun()
{
    // Compiler::Compile("code");
    // Runner::Run("code",1,1024*1024);

    Json::Value in_value;
    in_value["code"] = R"(#include <iostream>
        int main()
        {   
            qklqs
            //while(1);
            std::cout<<"hello CompileAndRun"<<std::endl;
        })";
    in_value["input"] = "";
    in_value["cpu_limit"] = 1;
    in_value["mem_limit"] = 5 * 10240;

    Json::FastWriter writer;
    std::string in_json = writer.write(in_value);

    std::string out_json;
    CompilerAndRunner::Start(in_json, &out_json);

    std::cout << out_json << std::endl;
}

int main(int argc,char *argv[])
{
    if(argc!=2)
    {
        std::cout<<argv[0]<<" + port"<<std::endl;

        return -1;
    }

    httplib::Server svr;

    svr.Post("/compile_and_run",[](const httplib::Request &req,httplib::Response &resp)
    {
        std::string in_json=req.body;
        std::string out_json;

        if(!in_json.empty())
        {
            CompilerAndRunner::Start(in_json, &out_json);
            resp.set_content(out_json, "application/json;charset=utf-8");
        }
    });

    // svr.Get("/hello",[](const httplib::Request &req,httplib::Response &resp)
    //     {
    //         resp.set_content("hello httplib,你好 httplib!", "text/plain;charset=utf-8");
    //     });

    svr.listen("0.0.0.0",std::atoi(argv[1]));

    return 0;
}